import { customForm } from "@/components/ProForm/interface/index";

import { Dictionary } from "@/api/interface/auth/basic";
import { defaultStyle } from "@/basic/style";

export function useAddForm(refresh: () => Promise<void>) {
  let addForm = reactive<customForm<Dictionary.ReqSave>>({
    form: {
      id: -1,
      dcode: "",
      dvalue: "",
      showOrder: "",
      remark: ""
    },
    title: "",
    minWidth: 660,
    labelWidth: [100, 120],
    visible: false,
    formAttrs: {},
    formCols: 1,
    options: [
      {
        label: "id",
        prop: "id",
        hide: true,
        type: "input"
      },
      {
        type: "input",
        label: "字典标识",
        hide: false,
        prop: "dcode",
        defaultValue: "",
        attrs: {
          disabled: true
        },
        rules: [{ required: true, message: "字典名称", trigger: "blur" }]
      },
      {
        type: "input",
        label: "dictionaryValue",
        hide: false,
        prop: "dvalue",
        defaultValue: "",
        rules: [{ required: true, message: "dictionaryValue", trigger: "blur" }],
        attrs: {
          placeholder: "字典值,多个以;分隔"
        }
      },
      {
        type: "input",
        label: "显示顺序",
        defaultValue: "",
        hide: false,
        prop: "showOrder",
        rules: [{ validator: validateNumber1, trigger: "blur" }]
      },
      {
        type: "input",
        label: "remark",
        defaultValue: "",
        hide: false,
        prop: "remark",
        attrs: {
          style: defaultStyle
        }
      }
    ]
  });

  const saveAddForm = async (form: Dictionary.ReqSave) => {
    await dictionarySave(form);
    addForm.visible = false;
    refresh();
  };
  return { addForm, saveAddForm };
}
